<?php /* COMPANIES $Id: view.php,v 1.48 2005/03/30 14:11:01 gregorerhardt Exp $ */
$company_id = intval( dPgetParam( $_GET, "company_id", 0 ) );

// check permissions for this record
$perms =& $AppUI->acl();
$canRead = $perms->checkModuleItem( $m, 'view', $company_id );
$canEdit = $perms->checkModuleItem( $m, 'edit', $company_id );


if (!$canRead) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

// retrieve any state parameters
if (isset( $_GET['tab'] )) {
	$AppUI->setState( 'CompVwTab', $_GET['tab'] );
}
$tab = $AppUI->getState( 'CompVwTab' ) !== NULL ? $AppUI->getState( 'CompVwTab' ) : 0;

// check if this record has dependencies to prevent deletion
$msg = '';
$obj = new CCompany();
$canDelete = $obj->canDelete( $msg, $company_id );

// load the record data
$q  = new DBQuery;
$q->addTable('companies', 'c');
$q->addQuery('c.*, u.user_id');
$q->addQuery('CONCAT(co.contact_first_name, " ", co.contact_last_name) AS contact_name');
$q->addJoin('users', 'u', 'u.user_id = c.company_owner');
$q->addJoin('contacts', 'co', 'u.user_contact = co.contact_id');
$q->addWhere('c.company_id = '.$company_id);
$sql = $q->prepare();
$q->clear();

$obj = null;
if (!db_loadObject( $sql, $obj )) {
	$AppUI->setMsg( 'Company' );
	$AppUI->setMsg( "invalidID", UI_MSG_ERROR, true );
	$AppUI->redirect();
} else {
	$AppUI->savePlace();
}

// load the list of project statii and company types
$pstatus = dPgetSysVal( 'ProjectStatus' );
$types = dPgetSysVal( 'CompanyType' );

// setup the title block
$titleBlock = new CTitleBlock( 'Company Information' );
if ($canEdit) {
	$titleBlock->addCell();
	$titleBlock->addButton($AppUI->_('new company'), 'index.php?m=companies&a=addedit');
}
$titleBlock->addButton( 'company list', 'index.php?m=companies' );
$titleBlock->show();
?>
<script language="javascript">
<?php
// security improvement:
// some javascript functions may not appear on client side in case of user not having write permissions
// else users would be able to arbitrarily run 'bad' functions
if ($canDelete) {
?>
function delIt() {
	if (confirm( "<?=$AppUI->_('doDelete').' '.$AppUI->_('company').' ['.$obj->company_name.'] ?';?>" )) {
		document.frmDelete.submit();
	}
}
<?php } ?>
</script>

<?php if ($canDelete) { ?>
<form name="frmDelete" action="./index.php?m=companies" method="post">
	<input type="hidden" name="dosql" value="do_company_aed" />
	<input type="hidden" name="del" value="1" />
	<input type="hidden" name="company_id" value="<?php echo $company_id;?>" />
</form>
<?php } ?>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td class="tabon"><?=$obj->company_name?></td><td width="100%"></td></tr>
</table>
<table border="0" cellpadding="7" cellspacing="0" width="100%" class="infopanel">
<tr>
	<td class="status"><?=$AppUI->_('Type')?>:</td>
	<td><?=$AppUI->_($types[@$obj->company_type])?></td>
	<td class="status"><?=$AppUI->_('Owner')?>:</td>
	<td nowrap="nowrap"><a href="javascript:User(<?=@$obj->user_id?>).show()"><?=@$obj->contact_name?></a></td>
	<td width="100%"></td>
	<td><?=showButton( 'edit', 'index.php?m=companies&a=addedit&company_id='.$company_id )?></td>
	<? if ($canDelete) { ?>
	<td><?=showButton('delete', 'js:delIt()')?></td>
	<? } ?>
</tr>
</table>
<table border="0" cellpadding="4" cellspacing="0" width="100%" class="subinfopanel">
<tr>
	<td valign="top" width="50%" height="100%">
		<table cellspacing="0" cellpadding="3" width="100%" class="list" height="100%">
		<tr class="info"><td colspan="4"><?=$AppUI->_('Contact information')?></td></tr>
		<tr class="second">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Email')?>:</td>
			<td width="100%" colspan="3"><a href="mailto:<?=$obj->company_email?>"><?=$obj->company_email?></a></td>
		</tr>
		<tr class="second">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Website')?>:</td>
			<td colspan="3">
				<a href="http://<?=@$obj->company_primary_url?>" target="Company"><?=@$obj->company_primary_url?></a>
			</td>
		</tr>
		<tr class="second" valign=top>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Address')?>:</td>
			<td colspan="3">
			<?php
				echo @$obj->company_address1
					.( ($obj->company_address2) ? ', '.$obj->company_address2 : '' )
					.( ($obj->company_city) ? ', '.$obj->company_city : '' )
					.( ($obj->company_state) ? ', '.$obj->company_state : '' )
					.( ($obj->company_zip) ? ', '.$obj->company_zip : '' );
			?>
			</td>
		</tr>
		<tr class="second">
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Phone')?>:</td>
			<td><?=@$obj->company_phone1.', '.@$obj->company_phone2?></td>
			<td align="right" nowrap="nowrap"><?=$AppUI->_('Fax')?>:</td>
			<td><?=@$obj->company_fax?></td>
		</tr>
		</table>
	</td>
	<td width="50%" valign="top" height="100%">
		<table cellspacing="0" cellpadding="2" border="0" width="100%" class="list" height="100%">
		<tr class="info"><td colspan="2"><?=$AppUI->_('Description')?></td></tr>
		<tr class="second">
			<td style="height: 100%"><?=str_replace(chr(10), '<br/>', $obj->company_description)?>&nbsp;</td>
		</tr>
		</table>
		<?php
			require_once("./classes/CustomFields.class.php");
			$custom_fields = New CustomFields( $m, $a, $obj->company_id, "view" );
			$custom_fields->printHTML();
		?>
	</td>
</tr>
</table>
<?php
// tabbed information boxes
$tabBox = new CTabBox( "?m=companies&a=view&company_id=$company_id", $tab );
$tabBox->add( 'vw_active', 'Active Projects' );
$tabBox->add( 'vw_archived', 'Archived Projects' );
$tabBox->add( 'vw_depts', 'Departments' );
$tabBox->add( 'vw_users', 'Users' );
$tabBox->add( 'vw_contacts', 'Contacts' );
$tabBox->loadExtras($m);
$tabBox->show();
?>
